package com.iotek.bookstore.modle.dao.impl;

import com.iotek.bookstore.modle.entity.User;
import com.iotek.bookstore.util.PreparedStatementSetter;
import com.iotek.bookstore.util.RsCallBackHandler;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl extends MyBookstoreDaoImpl<User> {

    /*
    * 添加用户信息
    * */
    @Override
    public int insert(final User user) {
        String sql="insert into user(email,username,password) values (?,?,?)";
        this.update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setString(1,user.getEmail());
                pstmt.setString(2,user.getUserName());
                pstmt.setString(3,user.getPassword());
            }
        });
        return 0;
    }

    /*
    * 根据用户Email查找用户信息
    * */
    @Override
    public List<User> query(final String str) {
        String sql="select * from user where email=?";
        this.query(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement pstmt) throws SQLException {
                pstmt.setString(1, str);
            }
        }, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
                createUser(rs);
            }
        });
        return list;
    }

    /*
    * 跟新用户信息
    * */
    @Override
    public int update(final User user) {
        if (user.getPassword() == null) {
            String sql = "update user set username=?,jurisdiction=? where id=?";
            return this.update(sql, new PreparedStatementSetter() {
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setString(1, user.getUserName());
                    pstmt.setInt(2, user.getJurisdiction());
                    pstmt.setInt(3, user.getId());
                }
            });
        } else {
            //用户自己修改自己的信息
            String sql = "update user set username=?,password=? where id=?";
            return this.update(sql, new PreparedStatementSetter() {
                public void setValues(PreparedStatement pstmt) throws SQLException {
                    pstmt.setString(1, user.getUserName());
                    pstmt.setString(2, user.getPassword());
                    pstmt.setInt(3, user.getId());
                }
            });

        }

    }
    /*
    * 查询所有的用户信息
    * */

    @Override
    public List<User> queryAll() {
        String sql="select * from user";
        this.query(sql, null, new RsCallBackHandler() {
            public void processRs(ResultSet rs) throws SQLException {
                createUser(rs);
            }
        });
        return list;
    }

    /*
    * 生成查询通用的User实例
    * */
    private void createUser(ResultSet rs)throws SQLException{
        while(rs.next()){
            User user=new User();
            user.setId(rs.getInt(1));
            user.setEmail(rs.getString(2));
            user.setUserName(rs.getString(3));
            user.setPassword(rs.getString(4));
            user.setJurisdiction(rs.getInt(5));
            list.add(user);
        }
    }
}
